using Application.Interface;
using Domain.Dto;
using Domain.Interface;
using Domain.Model;
using Domain.Specification;
using Gmmy.ApplicationExtension;
using Gmmy.DomainExtension;
using Gmmy.Utility;
using Microsoft.Extensions.Logging;
using Surging.Core.CPlatform.Ioc;
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Steeltoe.Extensions.Configuration.ConfigServer;

namespace Application.Services
{
    [ModuleName("SampleModel")]
    public class SampleModelService : BaseServices<SampleModel>, ISampleModelService
    {
        private readonly ISampleModelDomainService _domainService;
        private readonly ISampleModelRepository _repository;
        private readonly ILogger<SampleModelService> _logger;
        public SampleModelService(ISampleModelRepository repository, ISampleModelDomainService domainService
		,ILogger<SampleModelService> logger
		)
            : base(repository, logger)
        {
			_domainService = domainService;
            _repository = repository;
			_logger = logger;
        }

        /// <summary>
        /// 获取案例模型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> Get(GetInput input)
        {
            return await Do(input, async result =>
            {
                result.data =
                    await _repository.GetByConditionAsync(
                        new DirectSpecification<SampleModel>(x => !x.IsDeleted && x.ID == input.ID));
            });
        }

        /// <summary>
        /// 获取案例模型列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> GetList(SampleModelListInput input)
        {
            return await Do(input, async result =>
            {
                result.data = await _repository.GetList(input,new SampleModelListSpecification(input));
            });
        }

        /// <summary>
        /// 创建案例模型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> Create(SampleModelCreateInput input)
        {
            return await Do(input, async result =>
            {
                await _domainService.Create(input);
                await base.CommitAsync();
            });
        }

        /// <summary>
        /// 更新案例模型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> Update(SampleModelUpdateInput input)
        {
            return await Do(input, async result =>
            {
                await _domainService.Update(input);
                await base.CommitAsync();
            });
        }

        /// <summary>
        /// 逻辑删除案例模型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<CustomApiResult> LogicDelete(LogicDeleteInput input)
        {
            return await Do(input, async result =>
            {
                _repository.LogicDelete(new DirectSpecification<SampleModel>(x => input.DeletedIds.Contains(x.ID)));
                await base.CommitAsync();
            });
        }
    }
}